<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'bas:cus:basCusKpinfo:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="40%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>

    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasCusBasCusKpinfoForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { BasCusKpinfo, basCusKpinfoSave, basCusKpinfoForm, basUserCusTree } from '/@/api/bas/cus/basCusKpinfo';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('bas.cus.basCusKpinfo');
  const { showMessage } = useMessage();
  const record = ref<BasCusKpinfo>({} as BasCusKpinfo);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增开票信息') : t('编辑开票信息'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
        label: t('客户名称'),
        field: 'cusCode',
        fieldLabel: 'basCus.name',
        component: 'TreeSelect',
        componentProps: {
          api: basUserCusTree,
          params: {
            isAll:true,
          },
          canSelectParent: false,
          allowClear: true,
          immediate: true,
        },
        required: true,
        colProps: { lg: 24, md: 24 },
        itemProps: { validateTrigger: 'blur' }
      },
    // {
    //   label: t('客户编码'),
    //   field: 'cusCode',
    //   component: 'Input',
    //   // dynamicDisabled:true,
    //   componentProps: {
    //     maxlength: 100,
    //   },
    //   required: true,
    // },
    // {
    //   label: t('客户名称'),
    //   field: 'basCus.name',
    //   component: 'Input',
    //   dynamicDisabled:true,
    //   componentProps: {
    //     maxlength: 100,
    //   },
      
    // },
    {
      label: t('抬头类型'),
      field: 'ctype',
      component: 'RadioGroup',
      defaultValue:'2',
      componentProps: {
        dictType: 'cus_fp_type',
      },
      required: true,
    },
    {
      label: t('名称'),
      field: 'cname',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
      required: true,
    },
    {
      label: t('税号'),
      field: 'dutyNo',
      component: 'Input',
      ifShow:(e)=>{
        return e.model.ctype == '2'?true:false
      },
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('联系方式'),
      field: 'contactInfo',
      helpMessage: '邮箱/手机任选一项填写',
      component: 'Input',
      componentProps: {
        maxlength: 100,
        placeholder: t('邮箱/手机任选一项填写'),
      },
      required: true,
    },
    {
      label: t('是否默认'),
      field: 'bdef',
      component: 'Switch',
      // defaultValue:true,
      // componentProps: {
      //   dictType: 'sys_yes_no',
      // },
      // required: true,
      // itemProps: { validateTrigger: 'blur' }
    },
    
    {
      label: t('开票单位信息 （以下非必填信息）'),
      field: 'bankInfo',
      component: 'FormGroup',
      colProps: { lg: 24, md: 24 },
      ifShow:(e)=>{
        return e.model.ctype == '2'?true:false
      },
    },
    {
      label: t('开户银行'),
      field: 'bankName',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      ifShow:(e)=>{
        return e.model.ctype == '2'?true:false
      },
    },
    {
      label: t('银行帐号'),
      field: 'bankCode',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      ifShow:(e)=>{
        return e.model.ctype == '2'?true:false
      },
    },
    {
      label: t('单位号码'),
      field: 'dwNo',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      ifShow:(e)=>{
        return e.model.ctype == '2'?true:false
      },
    },
    {
      label: t('单位地址'),
      field: 'dwAddr',
      component: 'Input',
      componentProps: {
        maxlength: 500,
      },
      ifShow:(e)=>{
        return e.model.ctype == '2'?true:false
      },
    },
    
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await basCusKpinfoForm(data);
    record.value = (res.basCusKpinfo || {}) as BasCusKpinfo;
    record.value.__t = new Date().getTime();
    record.value.bdef = record.value.bdef == '1' ? true : false;
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      let data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      data = {
        ...data,
        bdef:data.bdef?'1':'0',
        dutyNo:data.ctype == '2'?data.dutyNo:'',
        bankName:data.ctype == '2'?data.bankName:'',
        bankCode:data.ctype == '2'?data.bankCode:'',
        dwNo:data.ctype == '2'?data.dwNo:'',
        dwAddr:data.ctype == '2'?data.dwAddr:'',
      }
      // console.log('submit', params, data, record);
      const res = await basCusKpinfoSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
